<?php
/** Zend_Controller_Action */ 
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';
class ModelfieldController extends Soooner_Controller_Action
{
	private $_fieldid;
	private $_modelModelfield;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelModelfield = new Model_Tables_Modelfield();
    }
   /**
    * 获取模型字段列表
    * @return unknown_type
    */
    public function indexAction()
    {
    	//$numPerPage = 20;//每页显示的条数
        //$ItemCount =  $this->_modelModelfield->getCount();
        //$PageCount = ceil($ItemCount/$numPerPage);
        //$PageCount = $PageCount ? $PageCount : 1;
        $t = $this->_getParam('t');//模型表名
        $modelid = $this->_getParam('modelid');
		include $this->_dzTemplate->getFile( 'modelfield.manage.html' );
    }
    /**
    * 获取模型字段列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
    	$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
    	$t = $this->_getParam('t');//模型表名
    	$modelid = $this->_getParam('modelid');        
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		$s = $this->_getParam('s'); 
        $ItemCount =  $this->_modelModelfield->getCount($s,$modelid);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos = $this->_modelModelfield->getList( $page, $numPerPage,$s,$modelid );
        include $this->_dzTemplate->getFile( 'ajax/modelfield.manage.ajax.html' );
    }
    /**
    * 添加模型字段
    * @return unknown_type
    */
    public function addAction()
    {
    	$request = $this->getRequest(); 
    	$modelid = $this->_getParam('modelid');
    	$t = $this->_getParam('t'); //表名
    	$modelfieldtype = $this->_modelModelfield->getModelFieldType();
        if ($request->isPost())
        {
            $params =$request->getPost();
            $this->_modelModelfield->add($params,$t);                
            $this->_redirect("/modelfield/index/t/$t/modelid/$modelid");
        } 
        include $this->_dzTemplate->getFile( 'modelfield.add.html' );
    }
    /**
    * 修改模型字段
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$modelid = $this->_getParam('modelid');
    	$fieldid = $this->_getParam('fieldid');
    	$info = $this->_modelModelfield->getInfo($fieldid); 
        $request = $this->getRequest();   
        $modelfieldtype = $this->_modelModelfield->getModelFieldType();     
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $this->_modelModelfield->modify($request,$fieldid);
                $this->_redirect("/modelfield/index/modelid/$modelid");
            }
        } 
        include $this->_dzTemplate->getFile( 'modelfield.edit.html' );
    }
    /**
    * 无刷新修改模型字段
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $fieldid = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelModelfield->modifyAjax($fieldid,$field,$value);
        echo $value;
        exit;
    }
    /**
    * 删除模型字段
    */
    public function deleteAction()
    {
        $fieldid = $this->_getParam('fieldid'); 
    	$t = $this->_getParam('t');   
    	$field = $this->_getParam('field');   
        $this->_modelModelfield->del($fieldid,$t,$field); 
        echo 0;
        exit;
        //$this->_redirect("/modelfield/");
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelModelfield->modifyAjaxRevers($field,$pri);
        exit;
    }
    
}